<template>
  <el-table :data="PermissionArr" style="width: 100%; margin-bottom: 20px" row-key="id" border>
    <el-table-column label="名称" prop="name"></el-table-column>
    <el-table-column label="权限值" prop="code"></el-table-column>
    <el-table-column label="修改时间" prop="updateTime"></el-table-column>
    <el-table-column label="操作" width="260px">
      <template #="{ row, $index }">
        <el-button type="primary" size="small" @click="addPermission(row)" :disabled="row.level == 4 ? true : false">{{
          row.level == 3 ? '添加功能' :
            '添加菜单' }}</el-button>
        <el-button type="primary" size="small" @click="updatePermission(row)"
          :disabled="row.level == 1 ? true : false">编辑</el-button>
        <el-popconfirm :title="`你确定要删除${row.name}吗`" width="260px" @confirm="removeMenu(row.id)">
          <template #reference>
            <el-button type="danger" size="small" :disabled="row.level == 1 ? true : false">删除</el-button>
          </template>
        </el-popconfirm>

      </template>
    </el-table-column>
  </el-table>
  <!-- 对话框组件：添加或者更新已有的菜单的数据结构 -->
  <el-dialog v-model="dialogVisible" :title="menuData.id ? '更新菜单' : '添加菜单'" width="500">
    <!-- 表单组件 -->
    <el-form ref="form" :rules="rules" :model="menuData" label-width="100px">
      <el-form-item label="名称" prop="name">
        <el-input placeholder="请你输入菜单名称" v-model="menuData.name"></el-input>
      </el-form-item>
      <el-form-item label="权限" prop="code">
        <el-input placeholder="请你输入权限值" v-model="menuData.code"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup lang='ts' name='permission'>
import { reqAddOrUpdateMenu, reqAllPermission, reqRemoveMenu } from '@/api/acl/menu';
import type { MenuParams, Permission, PermissionList, PermissionResponseData } from '@/api/acl/menu/type';
import { ElMessage } from 'element-plus';
import { nextTick, onMounted, reactive, ref } from 'vue';
let PermissionArr = ref<PermissionList>([])
let dialogVisible = ref<boolean>(false)
let menuData = reactive<MenuParams>({
  code: '',
  level: 0,
  name: '',
  pid: 0
})
//表单组件实例
let form = ref<any>()
onMounted(() => {
  getHasPermisstion()
})
//获得菜单与权限
const getHasPermisstion = async () => {
  let result: PermissionResponseData = await reqAllPermission()
  if (result.code == 200) {
    PermissionArr.value = result.data
  }
}
//添加菜单按钮回调
const addPermission = (row: Permission) => {
  nextTick(() => {
    form.value.clearValidate('name')
    form.value.clearValidate('code')
  })
  //清空数据
  Object.assign(menuData, {
    code: '',
    level: 0,
    name: '',
    pid: 0,
    id: 0
  })
  dialogVisible.value = true
  //收集数据
  menuData.level = row.level + 1
  menuData.pid = row.id
}
//编辑菜单按钮回调
const updatePermission = (row: Permission) => {
  dialogVisible.value = true
  Object.assign(menuData, row)
}
//添加|更新对话框确定按钮回调
const save = async () => {
  await form.value.validator()
  let result: any = await reqAddOrUpdateMenu(menuData)
  if (result.code == 200) {
    dialogVisible.value = false
    ElMessage({ type: 'success', message: menuData.id ? '更新成功' : '添加成功' })
    getHasPermisstion()
  } else {
    dialogVisible.value = false
    ElMessage({ type: 'error', message: menuData.id ? '更新失败' : '添加失败' })
  }
}
// 表单校验
const validatorname = (rule: any, value: any, callBack: any) => {
  if (value.trim().length >= 2) {
    callBack()
  } else {
    callBack(new Error('密码至少2位'))
  }
}
const validatorcode = (rule: any, value: any, callback: any) => {
  if (value.trim().length >= 2) {
    callback()
  } else {
    callback(new Error('权限名字长度需要大于2'))
  }
}


const rules = reactive({
  name: [{ required: true, trigger: 'blur', validator: validatorname }],
  code: [{ required: true, trigger: 'blur', validator: validatorcode }],
})

const removeMenu = async (id: number) => {
  let result: any = await reqRemoveMenu(id)
  if (result.code == 200) {
    ElMessage({ type: 'success', message: '删除成功' })
    getHasPermisstion()
  }
}
</script>


<style scoped></style>